package api

import (
	"context"

	controller "gitee.com/klein-x/biz/controller/bank"
	"gitee.com/klein-x/biz/xutils"
	"github.com/cloudwego/hertz/pkg/app"
)

type QuestionInfoRequest struct {
	Version    Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	QuestionId string  `form:"question_id" json:"question_id"`
}
type QuestionInfoResponse struct {
	Question *controller.QuestionInfo `json:"question"`
}

// 获取全局试题信息
func GetQuestionInfo(ctx context.Context, c *app.RequestContext) {
	var req QuestionInfoRequest
	var res QuestionInfoResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	if info, err := controller.GetQuestionInfo(req.QuestionId); err != nil {
		ReturnError(c, err)
	} else {
		res.Question = info
		ReturnSuccess(c, res)
	}
}

type UserChapterRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
}
type UserChapterResponse struct {
	Chapter []*controller.ChapterSummaryInfo `json:"user_chapter"`
}

// 获取用户章节列表
func GetUserChapter(ctx context.Context, c *app.RequestContext) {
	var req UserChapterRequest
	var res UserChapterResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if chapterSummaryList, err := controller.GetUserChapterInfos(uid); err != nil {
		ReturnError(c, err)
	} else {
		res.Chapter = chapterSummaryList
		ReturnSuccess(c, res)
	}
}

type UserChapterDetailRequest struct {
	Version   Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	ChapterID string  `form:"chapter_id" json:"chapter_id"`
}
type UserChapterDetailResponse struct {
	Chapter []*controller.ChapterInfo `json:"chapter"`
}

// 获取章节列表详情
func GetChapterDetail(ctx context.Context, c *app.RequestContext) {
	var req UserChapterDetailRequest
	var res UserChapterDetailResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	if chapterList, err := controller.GetChapterInfos(req.ChapterID); err != nil {
		ReturnError(c, err)
	} else {
		res.Chapter = chapterList
		ReturnSuccess(c, res)
	}
}

type ExamInfoRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	PaperID string  `form:"paper_id" json:"paper_id"`
}
type ExamInfoResponse struct {
	ExamInfo *controller.ExamInfo `json:"exam"`
}

// 获取用户考试信息
func GetUserExamInfo(ctx context.Context, c *app.RequestContext) {
	var req ExamInfoRequest
	var res ExamInfoResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if ExamInfo, err := controller.GetExamInfo(uid, req.PaperID); err != nil {
		ReturnError(c, err)
	} else {
		res.ExamInfo = ExamInfo
		ReturnSuccess(c, res)
	}
}

type UserInitExamRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	PaperId string  `form:"paper_id" json:"paper_id"`
}
type UserInitExamResponse struct {
	Status string `json:"status"`
}

// 重置用户答题卡
func ResetUserExam(ctx context.Context, c *app.RequestContext) {
	var req UserInitExamRequest
	var res UserInitExamResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if err := controller.ResetUserExam(uid, req.PaperId); err != nil {
		ReturnError(c, err)
	} else {
		res.Status = "sucess"
		ReturnSuccess(c, res)
	}
}

type UserKeepExamRequest UserInitExamRequest
type UserKeepExamResponse struct {
	CurrQuestionId string `json:"curr_question_id"`
}

// 用户开始答题
func BeginUserExam(ctx context.Context, c *app.RequestContext) {
	var req UserKeepExamRequest
	var res UserKeepExamResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if CurrQuestionId, err := controller.BeginUserExam(uid, req.PaperId); err != nil {
		ReturnError(c, err)
	} else {
		res.CurrQuestionId = CurrQuestionId
		ReturnSuccess(c, res)
	}
}

type UserSetAnswerRequest struct {
	Version    Version  `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	PaperId    string   `form:"paper_id" json:"paper_id"`
	QuestionId string   `form:"question_id" json:"question_id"`
	Answer     []string `form:"answer" json:"answer"`
}

// 记录用户答题记录
func SetUserAnswer(ctx context.Context, c *app.RequestContext) {
	var req UserSetAnswerRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if err := controller.SetUserAnswer(uid, req.PaperId, req.QuestionId, req.Answer); err != nil {
		ReturnError(c, err)
	} else {
		ReturnSuccess(c, nil)
	}
}

type UserGetAnswerRequest struct {
	Version    Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	PaperId    string  `form:"paper_id" json:"paper_id"`
	QuestionId string  `form:"question_id" json:"question_id"`
}
type UserGetAnswerResponse struct {
	Answer []string `json:"answer"`
}

// 获取用户答题记录
func GetUserAnswer(ctx context.Context, c *app.RequestContext) {
	var req UserGetAnswerRequest
	var res UserGetAnswerResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if Answer, err := controller.GetUserAnswer(uid, req.PaperId, req.QuestionId); err != nil {
		ReturnError(c, err)
	} else {
		res.Answer = Answer
		ReturnSuccess(c, res)
	}
}

type UserSetQuestionNoteRequest struct {
	Version    Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	PaperId    string  `form:"paper_id" json:"paper_id"`
	QuestionId string  `form:"question_id" json:"question_id"`
	Note       string  `form:"note" json:"note"`
}

// 设置用户笔记
func SetUserQuestionNote(ctx context.Context, c *app.RequestContext) {
	var req UserSetQuestionNoteRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if err := controller.SetUserQuestionNote(uid, req.PaperId, req.QuestionId, req.Note); err != nil {
		ReturnError(c, err)
	} else {
		ReturnSuccess(c, nil)
	}
}

type UserGetQuestionNoteRequest struct {
	Version    Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	PaperId    uint    `form:"paper_id" json:"paper_id"`
	QuestionId uint    `form:"question_id" json:"question_id"`
}
type UserGetQuestionNoteResponse struct {
	Note string `json:"note"`
}

// 获取用户笔记
func GetUserQuestionNote(ctx context.Context, c *app.RequestContext) {
	var req UserSetQuestionNoteRequest
	var res UserGetQuestionNoteResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if note, err := controller.GetUserQuestionNote(uid, req.PaperId, req.QuestionId); err != nil {
		ReturnError(c, err)
	} else {
		res.Note = note
		ReturnSuccess(c, res)
	}
}

type UserSetFavoriteRequest struct {
	Version    Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	PaperId    string  `form:"paper_id" json:"paper_id"`
	QuestionId string  `form:"question_id" json:"question_id"`
	IsFavorite bool    `form:"is_favorite" json:"is_favorite"`
}

// 收藏题库试题
func SetUserQuestionFavorite(ctx context.Context, c *app.RequestContext) {
	var req UserSetFavoriteRequest
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if err := controller.SetUserQuestionFavorite(uid, req.PaperId, req.QuestionId, req.IsFavorite); err != nil {
		ReturnError(c, err)
	} else {
		ReturnSuccess(c, nil)
	}
}

type UserFavoriteInfoRequest struct {
	Version    Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
	PaperId    string  `form:"paper_id" json:"paper_id"`
	QuestionId string  `form:"question_id" json:"question_id"`
}
type UserFavoriteInfoResponse struct {
	IsFavorite bool `json:"is_favorite"`
}

// 获取收藏列表
func GetUserFavoriteInfo(ctx context.Context, c *app.RequestContext) {
	var req UserFavoriteInfoRequest
	var res UserFavoriteInfoResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if FavoriteInfos, err := controller.GetUserFavoriteInfo(uid, req.PaperId, req.QuestionId); err != nil {
		ReturnError(c, err)
	} else {
		res.IsFavorite = FavoriteInfos
		ReturnSuccess(c, res)
	}
}

type UserFavoriteInfosRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
}
type UserFavoriteInfosResponse struct {
	FavoriteInfos *[]controller.FavoriteInfo `json:"favorite_infos"`
}

// 获取收藏列表
func GetUserFavoriteInfos(ctx context.Context, c *app.RequestContext) {
	var req UserFavoriteInfosRequest
	var res UserFavoriteInfosResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if FavoriteInfos, err := controller.GetUserFavoriteInfos(uid); err != nil {
		ReturnError(c, err)
	} else {
		res.FavoriteInfos = FavoriteInfos
		ReturnSuccess(c, res)
	}
}

type UserWrongInfosRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
}
type UserWrongInfosResponse struct {
	WorngInfos *[]controller.WorngInfo `json:"worng_infos"`
}

// 获取错题列表
func GetUserWrongInfos(ctx context.Context, c *app.RequestContext) {
	var req UserWrongInfosRequest
	var res UserWrongInfosResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if WorngInfos, err := controller.GetUserWorngInfos(uid); err != nil {
		ReturnError(c, err)
	} else {
		res.WorngInfos = WorngInfos
		ReturnSuccess(c, res)
	}
}

type UserNoteInfosRequest struct {
	Version Version `path:"version" vd:"in($, 1, 2); msg:'未定义的请求'"`
}
type UserNoteInfosResponse struct {
	NoteInfos *[]controller.NoteInfo `json:"note_infos"`
}

// 获取收藏列表
func GetUserNoteInfos(ctx context.Context, c *app.RequestContext) {
	var req UserNoteInfosRequest
	var res UserNoteInfosResponse
	if err := c.BindAndValidate(&req); err != nil {
		ReturnError(c, xutils.Error(API_PARAM_ERROR, err))
		return
	}

	uid := c.GetString("id")

	if NoteInfos, err := controller.GetUserNoteInfos(uid); err != nil {
		ReturnError(c, err)
	} else {
		res.NoteInfos = NoteInfos
		ReturnSuccess(c, res)
	}
}
